"user strict";
//
const Controller = require('egg').Controller;
class ProductController extends Controller {
    async index() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            name,
            pingpai,
            phone_type,
            page = 1,
            number = 6
        } = ctx.request.query;
        // 如果page=2,则offset = 2,翻页为第二页,这个page是根据后台传进来的
        const offset = (page - 1) * number;
        let data, count; //data数据 ， count全部数据的数量
        let lujin = ''; //空的字符串
        let boolean = false;
        console.log(name)
        if (id || name && page || name || phone_type || pingpai) {
            if (name && page) {
                lujin = `name like '%${name}%'`;
                count = await app.mysql.query(`select count(*) as count from product where ${lujin}`);
                if (!count[0].count) {
                    lujin = `phone_type like '%${name}%'`;
                }
            }
            if (id) {
                lujin ? lujin += `and id like '${id}' ` : lujin += `id like '${id}' `;
            }
            if (phone_type) {
                lujin ? lujin += `and phone_type like '%${phone_type}%' ` : lujin += `phone_type like '%${phone_type}%' `;
            }
            if (pingpai) {
                lujin ? lujin += `and pingpai like '%${pingpai}%' ` : lujin += `pingpai like '%${pingpai}%' `;
            }
            boolean = true;
        }
        if (boolean) {
            data = await app.mysql.query(`select * from product where ${lujin} limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from product where ${lujin}`);
        } else {
            data = await app.mysql.query(`select * from product limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from product`);
        }

        if (count[0].count != 0) {
            ctx.body = {
                code: 0,
                msg: '查询成功',
                data: data,
                count: count[0].count,
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: '查询失败，没有数据',
            }
        }
    }
    async pick() { //配合修改数据的使用

        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.get('product', {
            ID: id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: '查找成功',
                data: result
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "查找失败"
            }
        }
    }
    async create() { //新增
        const {
            ctx,
            app
        } = this;
        // 从web页面传过来的数据
        const {
            name,
            phone_type,
            phone_size,
            electric,
            thickness,
            picture,
            price,
            pingpai,
            parameter,
            yingxiang,
            Endurance,
            star
        } = ctx.request.body;
        // let star;

        // const result = await app.mysql.query(`select  Score,appearance,performance,price from comment where product_name like "${name}" `);
        // console.log(45645645);
        // console.log(result);
        // star = Math.floor((result[0].Score+result[0].appearance+result[0].performance+result[0].price)/4);
        // console.log(star);

        const reg = /[1-5]{1}/,
            reg1 = /[0-9][\.]*/;
        const keyname = name.replace(/\s/g, '');
        if (name && phone_type && phone_size && electric && thickness && picture && price && pingpai && parameter && yingxiang && Endurance && star) {
            if (reg.test(star) && reg1.test(price)) {
                const result = await app.mysql.query(`select name from product`)
                // 如果查找的名字一样,则不能添加同名的管理员
                const result1 = result.filter(item => item.name.replace(/\s/g, '') == keyname);
                if (result1.length !== 0) {
                    ctx.body = {
                        code: 4006,
                        msg: "添加失败,已有此管理员",
                    };
                } else {
                    const row = {
                        name,
                        phone_type,
                        phone_size,
                        electric,
                        thickness,
                        picture,
                        price,
                        pingpai,
                        parameter,
                        yingxiang,
                        Endurance,
                        star
                    }
                    // 插入数据库的 product
                    const data = await app.mysql.insert('product', row);
                    ctx.body = {
                        code: 0,
                        msg: "添加成功",
                        data: row
                    };
                }
            } else {
                ctx.body = {
                    code: 4005,
                    msg: '请输入 1-5 之间的分数'
                }
            }
        } else {
            ctx.body = {
                code: 4008,
                msg: "缺少默认参数"
            }
        }
    }
    async update() { //修改
        // console.log('修改456654543212');
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params; //获取需要修改的数据id
        const {
            name,
            phone_type,
            phone_size,
            electric,
            thickness,
            picture,
            price,
            pingpai,
            parameter,
            yingxiang,
            Endurance,
            star
        } = ctx.request.body;
        const keyname = name.replace(/\s/g, '');
        const row = {
            name: keyname,
            phone_type: phone_type,
            phone_size: phone_size,
            electric: electric,
            thickness: thickness,
            picture: picture,
            price: price,
            pingpai: pingpai,
            parameter: parameter,
            yingxiang: yingxiang,
            Endurance: Endurance,
            star: star
        }
        const options = {
            // 将要修改的数据,放到获取的id那一列数据
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('product', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: "编辑成功",
            };
        } else {
            ctx.body = {
                code: 4008,
                msg: "编辑失败",
            };
        }
    }
    // 删除
    async destroy() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.delete(`product`, {
            id: +id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            };
        }
    }
}
module.exports = ProductController;